package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import entity.sparemanage.SpareTypes;
import com.buba.mapper.SpareTypesMapper;
import com.buba.service.SpareTypesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.sparemanage.dto.selAllSpareTypesByLimitDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.PageResult;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 备件类型 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class SpareTypesServiceImpl extends ServiceImpl<SpareTypesMapper, SpareTypes> implements SpareTypesService {

    @Autowired
    private SpareTypesMapper spareTypesMapper;

    /* *
     * @Author: beisheng
     * @Descriprion: 树形查询备件类型
     * @Date: 2023/5/19 15:20
     * @Return: common.R
     * @Param: []
     */

    @Override
    public List<SpareTypes> selAllSpareType() {
        List<SpareTypes> spareTypesList = spareTypesMapper.selectList(new QueryWrapper<SpareTypes>().eq("delete_flag", 0));
        List<SpareTypes> collect = spareTypesList.stream().filter(i ->
                Integer.valueOf(i.getParentId())  == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu,spareTypesList));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 分页模糊查寻备件类型
     * @Date: 2023/6/1 15:10
     * @Return: common.R
     * @Param: [typesByLimitDto]
     */

    @Override
    public PageResult selAllSpareTypesByLimit(selAllSpareTypesByLimitDto typesByLimitDto) {
        QueryWrapper<SpareTypes> spareTypesQueryWrapper  = new QueryWrapper<>();
        spareTypesQueryWrapper.eq("delete_flag",0);
        if (typesByLimitDto.getCodeName()!=null){
            String codeName = typesByLimitDto.getCodeName();
            spareTypesQueryWrapper.eq("sp_typename",codeName).or().eq("sp_typecode",codeName);
        }
        Page<SpareTypes> spareTypesPage = new Page<>(typesByLimitDto.getPage(),typesByLimitDto.getPageSize());
        Page<SpareTypes> spareTypesPages = spareTypesMapper.selectPage(spareTypesPage, spareTypesQueryWrapper);
        List<SpareTypes> spareTypesList = spareTypesMapper.selectList(new QueryWrapper<SpareTypes>().eq("delete_flag",0));
        List<SpareTypes> collect = spareTypesPages.getRecords().stream().filter(i ->
                Integer.valueOf(i.getParentId())  == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu,spareTypesPages.getRecords()));
            return menu;
        }).collect(Collectors.toList());
        return new PageResult(typesByLimitDto.getPage(),typesByLimitDto.getPageSize(),spareTypesList.size(),collect);
    }

    public List<SpareTypes> getChidrens(SpareTypes root,List<SpareTypes> all){
        List<SpareTypes> children  = all.stream().filter(carType -> {
            return Integer.valueOf(carType.getParentId()) == root.getSpId();
        }).map(carType -> {
            carType.setChildren(getChidrens(carType,all));
            return carType;
        }).collect(Collectors.toList());
        return children;
    }
}
